package com.qianli.cashmama.trade.mq;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.qianli.cashmama.trade.common.mq.MQTagEnum;
import com.qianli.cashmama.trade.common.mq.MQTradeActionEnum;
import com.qianli.cashmama.trade.common.mq.MQTradeActionVO;
import com.qianli.cashmama.trade.spring.SpringUtils;

/**
 * Author: Karl
 * Date: 2020/10/19 下午3:17
 */
public class MQOpenapiHelpUtils {

    private static MQHandlerService mqHandlerService;

    private static void checkAndSet() {
        if (mqHandlerService == null) {
            mqHandlerService = (MQHandlerService) SpringUtils.getBean("MQHandlerService");
        }
    }

    public static boolean send(String tradeNo, MQTradeActionEnum mqTradeActionEnum) {
        checkAndSet();
        send(tradeNo, mqTradeActionEnum, null);
        return true;
    }

    public static boolean send(String tradeNo, MQTradeActionEnum mqTradeActionEnum, Object extData) {
        checkAndSet();
        MQTradeActionVO MQTradeActionVO = new MQTradeActionVO();
        MQTradeActionVO.setTradeNo(tradeNo);
        MQTradeActionVO.setAction(mqTradeActionEnum.getCode());
        if (extData != null) {
            MQTradeActionVO.setExtendedData(JSON.toJSONString(extData));
        }
        mqHandlerService.sendMq(tradeNo, MQTagEnum.PUSH_ORDER_STATUS, JSONObject.toJSONString(MQTradeActionVO));
        return true;
    }

}
